import 'package:family_expense/features/journey/models/journey.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class JourneyService {
  final String _url = '/journey';
  final HttpRequestService _httpRequestService = HttpRequestService();

  /// Retrieves current account group users from backend service.
  Future<List<Journey>> queryListByAccountGroup(
      int pageSize, int pageIndex) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService.get(
        '$_url/query-list-by-account-group',
        params: {'pageSize': pageSize, 'pageIndex': pageIndex});

    // Parses the data from [Map<String, dynamic>] to [User].
    return (response.payload as List<dynamic>)
        .map((p) => Journey.fromJson(p))
        .toList();
  }

  /// Adds the new [Journey] to server.
  Future<bool> add(Journey journey) async {
    final response = await _httpRequestService.post(
      '$_url/add',
      body: journey.toJson(),
    );

    return response.success;
  }

  /// Updates the [Journey] info.
  Future<bool> update(Journey journey) async {
    final response =
        await _httpRequestService.post('$_url/update', body: journey.toJson());

    return response.success;
  }

  /// Deletes journey with [id].
  Future<bool> delete(String id) async {
    // Deletes data from server.
    final response =
        await _httpRequestService.post('$_url/delete', body: {'id': id});

    // Checks on result.
    final success = response.payload as bool;

    return success;
  }
}
